package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    View tab_Ctrl, tab_Set;
    //fragment管理器
    private FragmentManager fragmentManager;

    private ControlFragment controlFragment;
    private SettingFragment settingFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tab_Ctrl = findViewById(R.id.tab_Control);
        tab_Set = findViewById(R.id.tab_Setting);

        tab_Ctrl.setOnClickListener(this);
        tab_Set.setOnClickListener(this);
        FragementSwitch(1);

        tab_Ctrl.setBackgroundResource(R.color.bank);
        tab_Set.setBackgroundResource(R.color.chosecol);

         /* 初始化启动网络服务
        Intent intent=new Intent(this,TCPService.class);
        intent.putExtra("ipaddress",R.string.ipaddress1);
        intent.putExtra("port", R.string.comnumber1);
        startService(intent);*/
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tab_Control: {
                FragementSwitch(0);
                tab_Ctrl.setBackgroundResource(R.color.chosecol);
                tab_Set.setBackgroundResource(R.color.bank);
            }break;
            case R.id.tab_Setting: {
                FragementSwitch(1);

                tab_Ctrl.setBackgroundResource(R.color.bank);
                tab_Set.setBackgroundResource(R.color.chosecol);
            }break;
            default:break;
        }
    }
    /*
     *fragment切换方法
     *  index:界面号
     *      0:设备控制界面
     *      1:设置界面
     */
    private void FragementSwitch(int index) {
        //获取管理器
        fragmentManager = getSupportFragmentManager();
        //开始事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideAllFragemen(transaction);
        switch(index){
            case 0://设备控制界面
            {
                if(controlFragment==null){
                    controlFragment=new ControlFragment();
                    transaction.add(R.id.fragmentmain,controlFragment);
                }
                else{
                    transaction.show(controlFragment);
                    tab_Ctrl.setBackgroundResource(R.color.chosecol);
                    tab_Set.setBackgroundResource(R.color.bank);
                }
            }break;
            case 1://设置界面
            {
                if(settingFragment==null){
                    settingFragment=new SettingFragment();
                    transaction.add(R.id.fragmentmain,settingFragment);
                }
                else{
                    transaction.show(settingFragment);
                    tab_Ctrl.setBackgroundResource(R.color.bank);
                    tab_Set.setBackgroundResource(R.color.chosecol);
                }
            }break;
            default:break;
        }
        transaction.commit();
    }
    /*
     *fragment隐藏方法
     */
    private void hideAllFragemen(FragmentTransaction transaction) {

        if (controlFragment != null) {
            transaction.hide(controlFragment);
        }
        if (settingFragment != null) {
            transaction.hide(settingFragment);
        }
    }
}